import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.regex.Pattern;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Sun Dec 08 18:13:22 CST 2024
 */



/**
 * @author jjchen
 */
public class manager_register extends JFrame {
    public manager_register() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Login_Select loginSelect = new Login_Select();
        loginSelect.setVisible(true);
    }

    private void button1MouseClicked2(MouseEvent e) {

        String a = textField1.getText();
        char[] passwordChars = passwordField1.getPassword();
        String b = new String(passwordChars); // 注意：这样做可能不安全，应该使用加密/哈希
        String c;
        if (checkBox1.isSelected()) {
            c = checkBox1.getText();
        } else {
            c = checkBox2.getText();
        }
        String d = textField2.getText();
        String f = textField3.getText();

        if (a.isEmpty() || b.isEmpty() || c.isEmpty() || d.isEmpty() || f.isEmpty()) {
            JOptionPane.showMessageDialog(this, "注册失败请填写完整。");
            return;
        }
        // 验证ID格式：6到10位数字
        if (!Pattern.matches("^[0-9]{6,10}$", a)) {
            JOptionPane.showMessageDialog(this, "ID格式不正确，应为6到10位数字。");
            return;
        }

        // 验证密码格式：数字+字母
        if (!Pattern.matches("^(?=.*[0-9])(?=.*[a-zA-Z]).+$", b)) {
            JOptionPane.showMessageDialog(this, "密码格式不正确，应包含数字和字母。");
            return;
        }

        // 验证手机号格式：数字
        if (!Pattern.matches("^[0-9]+$", d)) {
            JOptionPane.showMessageDialog(this, "手机号格式不正确，应为数字。");
            return;
        }

        // 验证邮箱格式
        if (!Pattern.matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$", f)) {
            JOptionPane.showMessageDialog(this, "邮箱格式不正确。");
            return;
        }



        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String rootPassword = "root"; // 注意：不要在生产环境中使用硬编码的密码

        String sql = "INSERT INTO manager_information VALUES (?, ?, ?, ?, ?);";


        try (Connection conn = DriverManager.getConnection(url, username, rootPassword);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, a);
            stmt.setString(2, b);
            stmt.setString(3, c);
            stmt.setString(4, d);
            stmt.setString(5, f);

            int count = stmt.executeUpdate();
            System.out.println(count);

        } catch (SQLException ex) {
            // 处理SQL异常，例如记录日志或显示错误消息
            ex.printStackTrace();
        }

        // 注意：密码字符数组应该在使用后被清除，以防止内存中的密码泄露
        Arrays.fill(passwordChars, Character.MIN_VALUE);

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        passwordField1 = new JPasswordField();
        label4 = new JLabel();
        checkBox1 = new JCheckBox();
        checkBox2 = new JCheckBox();
        label5 = new JLabel();
        textField2 = new JTextField();
        label6 = new JLabel();
        textField3 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("C:\\workspace");
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[13,fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[15,left]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label1 ----
        label1.setText("\u7ba1\u7406\u5458\u6ce8\u518c");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | Font.BOLD, label1.getFont().getSize() + 3f));
        contentPane.add(label1, "cell 8 0 1 2");

        //---- label3 ----
        label3.setText("id");
        contentPane.add(label3, "cell 2 3 4 1");
        contentPane.add(textField1, "cell 5 3 8 1");

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2, "cell 2 4 4 1");
        contentPane.add(passwordField1, "cell 5 4 8 1");

        //---- label4 ----
        label4.setText("\u6027\u522b");
        contentPane.add(label4, "cell 2 5");

        //---- checkBox1 ----
        checkBox1.setText("\u7537");
        contentPane.add(checkBox1, "cell 5 5");

        //---- checkBox2 ----
        checkBox2.setText("\u5973");
        contentPane.add(checkBox2, "cell 8 5");

        //---- label5 ----
        label5.setText("\u624b\u673a\u53f7");
        contentPane.add(label5, "cell 2 6");
        contentPane.add(textField2, "cell 5 6 8 1");

        //---- label6 ----
        label6.setText("\u90ae\u7bb1");
        contentPane.add(label6, "cell 2 7");
        contentPane.add(textField3, "cell 5 7 8 1");

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
                button1MouseClicked2(e);
            }
        });
        contentPane.add(button1, "cell 6 9 4 1");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label3;
    private JTextField textField1;
    private JLabel label2;
    private JPasswordField passwordField1;
    private JLabel label4;
    private JCheckBox checkBox1;
    private JCheckBox checkBox2;
    private JLabel label5;
    private JTextField textField2;
    private JLabel label6;
    private JTextField textField3;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
